
// 封装api请求
import axios from 'axios'
// import { Message } from 'element-ui'

const baseUrl = process.env.BASE_URL

export const getRequest = (url, params) => {
  let path = ''
  if (Object.keys(params).length) {
    const query = getFormat(params)
    path = `${url}${query ? `?${query}` : ''}`
  }
  return axios({
    method: 'get',
    url: `${baseUrl}${path}`,
    params: {}
  })
}

export const postRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${baseUrl}${url}`,
    data: params
  })
}

const getFormat = params => {
  const arr = []
  if (params) {
    Object.keys(params).forEach(item => {
      arr.push(`${item}=${params[item]}`)
    })
  }
  return arr.join('&')
}

export const fd = (o = {}) => {
  const formData = new FormData()
  Object.keys(o).forEach(key => {
    const value = o[key]
    if (value || value === 0) {
      formData.append(key, value)
    }
  })
  return formData
}
